package com.spit.hkpost.viewmodel

import android.view.View
import androidx.databinding.ObservableInt
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.FileIOUtils
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.spit.hkpost.R
import com.spit.hkpost.bean.AppRoomDataBase
import com.spit.hkpost.bean.DataBean
import com.spit.hkpost.ext.Externalpath
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.BooleanObservableField
import me.hgj.jetpackmvvm.callback.databind.StringObservableField

/**
 * User: Nike
 *  2023/12/15 15:13
 */
class RfidParentModel : BaseViewModel() {


    var listBean = ArrayList<DataBean>()

    var parentOpenStatu: MutableLiveData<Boolean> = MutableLiveData()

    var mutableTitle: MutableLiveData<String> = MutableLiveData("HK Post")

    val configDao = AppRoomDataBase.get().getConfigDao()

    val rfidDao = AppRoomDataBase.get().getRfidDao()

    val isLoaddingBean: MutableLiveData<DataBean> = MutableLiveData()

    var searchText: MutableLiveData<String> = MutableLiveData()

    var epcUploadData: MutableLiveData<DataBean> = MutableLiveData()

    var listAllBean: MutableLiveData<ArrayList<DataBean>> = MutableLiveData()

    var listMatchBean: MutableLiveData<ArrayList<DataBean>> = MutableLiveData()

    var epcUploadPrefix: MutableLiveData<String> = MutableLiveData()

    var epcPrefix = StringObservableField("NO")

    var showProgress = BooleanObservableField(false)

    var today = StringObservableField(TimeUtils.getNowString(TimeUtils.getSafeDateFormat("yyyy-MM-dd")))

    fun getConfigPrefix(): String? {
        return runBlocking {
            withContext(Dispatchers.IO) {
                configDao.findFirst()?.prefix
            }
        }
    }

    var isSelectClear = BooleanObservableField()

    var backgroundColor = object : ObservableInt() {
        override fun get(): Int {
            return if (isSelectClear.get()) {
                R.color.red_ff0000
            } else {
                R.color.white_f4f4f4
            }
        }
    }

    var title = StringObservableField("HK POST")

    /*var title = object : ObservableInt(showProgress){
        override fun get(): Int {
            return if(password.get().isEmpty()){
                View.GONE
            }else{
                View.VISIBLE
            }
        }
    }*/

    fun onSave(data: List<DataBean>) {
        viewModelScope.launch(Dispatchers.IO) {
            var configBean = configDao.findFirst()
            var path = Externalpath + "/${configBean.rfidCode}" + "_${today.get()}" + ".txt"
            var sb = StringBuffer()
            sb.append("Created on: " + today.get()).append("\n")
            sb.append("Devices ID: " + configBean.rfidCode).append("\n")

            /*data.forEach {
                sb.append(it.crearDate).append(" IN   ").append("RFID TAG INFORMATION: ").append(it.tagId).append("\n")
            }*/

            rfidDao.findTimeAll(today.get()).forEach {
                sb.append(it.crearDate).append(" IN   ").append("RFID TAG INFORMATION: ").append(it.epc).append("\n")
            }

            val existsDir = FileUtils.createOrExistsDir(Externalpath)
            if (existsDir){
                FileUtils.listFilesInDir(Externalpath).forEach {
                    if (it.toString().contains(today.get())){
                        FileUtils.delete(it)
                    }
                }
                val b =
                    FileIOUtils.writeFileFromString(path, sb.toString())
                if (!b){
                    ToastUtils.showShort(appContext.getText(R.string.text9))
                }else{
                    ToastUtils.showShort(appContext.getText(R.string.release_success))
                    LogUtils.e(path)
                }
            }
        }
    }

}